<?php
include_once 'ZCollection.php';

class ZThreadCollection extends ZCollection {

    /**
     * @param $index Thread Index
     * @return ZThreadItem
     */
    public function getIndex($index){
        return $this->items[$index];
    }

    /**
     * @param $item ZThreadItem
     */
    public function addItem($item){
        $this->items[] = $item;
    }

    /**
     * Reformat threads have no title
     */
    public function formatValue(){
        foreach ($this->items as $value){
            if ($value->getThreadTitle() == ''){
                $value->setThreadTitle("No title");
            }
        }
    }
    /**
     * @param $arrPosts array[][]
     * @param $arrAuthors array[][]
     * @param $arrThumbTitle array
     * @param $top int select top
     * @param $length int Length of thumbnail Thread title
     */
    public function getTopThreads(&$arrPosts, &$arrAuthors, &$arrThumbTitle, $top, $length){
        for ($i = 0; $i < $top; $i++){
            $item = $this->getIndex($i);
            $title = $item->getThreadTitle();
            $url = $item->getThreadURL();

            if (mb_strlen($url, 'UTF-8') > 50){
                $url = mb_substr($url, 0, 50, 'UTF-8') . '...';
            }

            $arrPosts[] = array(
              'y'           => $item->getNumberOfPosts(),
              'fullTitle'   => $title,
              'fullUrl'     => $item->getThreadURL(),
              'url'         => $url
            );

            $arrAuthors[] = array(
              'y'           => $item->getNumberOfAuthors(),
              'fullTitle'   => $title,
              'fullUrl'     => $item->getThreadURL(),
              'url'         => $url
            );
            if (mb_strlen($title, 'UTF-8') > $length){
                $arrThumbTitle[] = mb_substr($title, 0, $length, 'UTF-8') . '...';
            }
            else{
                $arrThumbTitle[] = $title;
            }
        }
    }
}
?>